from urllib.request import Request,urlopen

def get_html(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
    }
    req = Request(url,headers=headers)
    rep = urlopen(req)
    return rep.read().decode('utf-8')

def save_html(html,filename):
    with open(filename,'w',encoding='utf-8')as f:
        f.write(html)


def main():
    for i in range(3):
        url = 'https://tieba.baidu.com/f?kw=%E5%81%A5%E8%BA%AB&ie=utf-8&pn={}'.format(i*50)
        # print(url)
        html = get_html(url)
        filename = "第"+str(i+1)+'页.html'
        save_html(html,filename)
if __name__=="__main__":
    main()


